/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package classcreator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

/**
 *
 * @author Stefano
 */
public class Field extends Parameter{
    private static double outputFormatVersion=1.0;
    
    public Visibility visibility=Visibility.priv;
    public String init="";
    /**
     * Adds '@Key' annotation
     */
    public boolean key=false;
    /**
     * Adds it in the default generated constructor
     */
    public boolean built=false;
    public boolean isStatic=false;
    
    
    public Field(Type t, String n){
        super(t,n);
    }
    
    public Field(Visibility vis, Type t, String n){
        super(t,n);
        this.visibility=vis;
    }
    
    public Field(Type t, String n, String i){
        super(t,n);
        init=i;
    }
    
    public Field(Visibility vis, Type t, String n, String i){
        super(t,n);
        init=i;
        this.visibility=vis;
    }
    
    public boolean isInitialized(){
        return !init.isEmpty();
    }
    
    public String toString(){
        if(isInitialized()){
            String s=super.toString();
            s=s+" = "+init+";";
            if(isStatic) s="static "+s;
            s=visibility.toString()+" "+s;
            if(key) s=Naming.keyParameterAnnotation+"\n"+s;
            return s;
        }
        else{
            String s=super.toString();
            s=s+";";
            if(isStatic) s="static "+s;
            s=visibility.toString()+" "+s;
            if(key) s=Naming.keyParameterAnnotation+"\n"+s;
            return s;
        }
    }
    
    public static Field parse(BufferedReader br, TypeRepository tr) throws IOException{
                br.readLine();
                double version=Double.parseDouble(br.readLine());
                
                if(version<=1.0){
                    Type t=Type.parse(br, tr);
                    String name=br.readLine();
                    Visibility vis=Visibility.parse(br.readLine());
                    String in=br.readLine();
                    boolean k=Boolean.parseBoolean(br.readLine());
                    boolean b=Boolean.parseBoolean(br.readLine());
                    boolean iss=Boolean.parseBoolean(br.readLine());
                    Field p= new Field(vis,t,name,in);
                    p.key=k;
                    p.built=b;
                    p.isStatic=iss;
                    return p;
                }
                //Actual version
                Type t=Type.parse(br, tr);
                String name=br.readLine();
                Visibility vis=Visibility.parse(br.readLine());
                String in=br.readLine();
                boolean k=Boolean.parseBoolean(br.readLine());
                boolean b=Boolean.parseBoolean(br.readLine());
                boolean iss=Boolean.parseBoolean(br.readLine());
                Field p= new Field(vis,t,name,in);
                p.key=k;
                p.built=b;
                p.isStatic=iss;
                return p;
    }
    
    public void permanentize(BufferedWriter br, TypeRepository tr) throws IOException{
        br.write("/***/ Field: "+name); br.newLine();
        br.write(String.valueOf(outputFormatVersion)); br.newLine();
                type.permanentize(br, tr); 
                br.write(name); br.newLine();
                br.write(visibility.toString()); br.newLine();
                br.write(init); br.newLine();
                br.write(String.valueOf(key)); br.newLine();
                br.write(String.valueOf(built)); br.newLine();
                br.write(String.valueOf(isStatic)); br.newLine();
    }
}
